package ui.commodity;

import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.List;
import java.util.Vector;

import javax.swing.JPanel;

import tool.MyButton;
import vo.CommodityTypeVo;
import vo.UserVo;
import vo.Vo;
import blservice.commodity.CommodityTypeBLService;

import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import client.boot.BLServiceManager;

public class CommodityTypeManagementPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private CommodityTypeBLService commodityTypeBL;
	private UserVo user;
	private JTable table;
	private JScrollPane jsp;
	private Vector<String> columnNames;

	public CommodityTypeManagementPanel(UserVo user) {

		try {
			commodityTypeBL = BLServiceManager.getCommodityTypeBLService(null);
		} catch (RemoteException | MalformedURLException | NotBoundException
				| SQLException e1) {
			JOptionPane.showMessageDialog(this, e1.getMessage());
			e1.printStackTrace();
		}

		this.setLayout(null);
		this.user = user;
		this.setOpaque(false);

		columnNames = new Vector<String>();

		columnNames.add("编号");
		columnNames.add("名称");
		columnNames.add("父节点");

		showAll();

		JButton btnAdd = new MyButton("Graphics/Button93-23.png");
		btnAdd.setText("添加");
		btnAdd.addActionListener(e -> addCommodityType());
		btnAdd.setBounds(88, 390, 93, 23);
		add(btnAdd);

		JButton btnDelete =new MyButton("Graphics/Button93-23.png");
		btnDelete.setText("删除");
		btnDelete.addActionListener(e -> deleteCommodityType());
		btnDelete.setBounds(276, 390, 93, 23);
		add(btnDelete);

		JButton btnUpdate =new MyButton("Graphics/Button93-23.png");
		btnUpdate.setText("修改");
		btnUpdate.addActionListener(e -> updateCommodityType());
		btnUpdate.setBounds(464, 390, 93, 23);
		add(btnUpdate);
	}

	private void addCommodityType() {
		try {
			int row = table.getSelectedRow();
			if(row==-1){
				return;
			}
			String name = (String) table.getValueAt(row, 1);
			if(name.equals("")){
				JOptionPane.showMessageDialog(this, "名称不能为空\n");
				return;
			}
			String father = (String) table.getValueAt(row, 2);	
			CommodityTypeVo addVo = new CommodityTypeVo();
			addVo.setOperator(user.getName());
			addVo.setOperation("添加商品分类");
			addVo.set名称(name);
			addVo.set父节点(father);
	
			Vo vo=commodityTypeBL.addCommodityType(addVo);
			
			if (vo.isSuccessed()) {
				JOptionPane.showMessageDialog(this, "添加成功\n");
				showAll();
			} else {
				JOptionPane.showMessageDialog(this,
						"添加失败\n" + vo.getFailMessage());
			}

			
		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(this, "无法连接到服务器\n" + e.getMessage());
			e.printStackTrace();
		}
	}

	private void deleteCommodityType() {
		try {
			int row = table.getSelectedRow();
			if(row==-1){
				return;
			}
			String name = (String) table.getValueAt(row, 1);
			// System.out.println(name);
			CommodityTypeVo deleteVo = new CommodityTypeVo();
			deleteVo.setOperator(user.getName());
			deleteVo.setOperation("删除商品分类");
			deleteVo.set名称(name);
			Vo vo = commodityTypeBL.deleteCommodityType(deleteVo);
			if (vo.isSuccessed()) {
				JOptionPane.showMessageDialog(this, "删除成功\n");
				showAll();
			} else {
				JOptionPane.showMessageDialog(this,
						"删除失败\n" + vo.getFailMessage());
			}
			
		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(this, "无法连接到服务器\n" + e.getMessage());
			e.printStackTrace();
		}
	}

	private void updateCommodityType() {
		try {
			int row = table.getSelectedRow();
			if(row==-1){
				return;
			}
			String identifier = (String) table.getValueAt(row, 0);
			String name = (String) table.getValueAt(row, 1);
			String father = (String) table.getValueAt(row, 2);
			if(name.equals("")){
				JOptionPane.showMessageDialog(this, "名称不能为空\n");
				return;
			}

			CommodityTypeVo updateVo = new CommodityTypeVo();
			updateVo.setOperator(user.getName());
			updateVo.setOperation("修改商品分类");
			updateVo.set编号(identifier);
			updateVo.set名称(name);
			updateVo.set父节点(father);
			

			Vo vo = commodityTypeBL.updateCommodityType(updateVo);

			if (vo.isSuccessed()) {
				JOptionPane.showMessageDialog(this, "修改成功\n");
				showAll();
			} else {
				JOptionPane.showMessageDialog(this,
						"修改失败\n" + vo.getFailMessage());
			}
		
		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(this, "无法连接到服务器\n" + e.getMessage());
			e.printStackTrace();
		}
	}

	public void showAll() {
		try {
			Vector<Vector<String>> rowData = new Vector<Vector<String>>();
			Vector<String> commodityTypeInfo;
			List<CommodityTypeVo> commodityTypeItem = commodityTypeBL.getAll();
			for (int i = 0; i < commodityTypeItem.size(); i++) {
				commodityTypeInfo = new Vector<String>();
				commodityTypeInfo.add(commodityTypeItem.get(i).编号);
				commodityTypeInfo.add(commodityTypeItem.get(i).名称);
				commodityTypeInfo.add(commodityTypeItem.get(i).父节点);			
				rowData.add(commodityTypeInfo);
			}
			commodityTypeInfo = new Vector<String>();
			commodityTypeInfo.add(null);
			commodityTypeInfo.add(null);
			commodityTypeInfo.add(null);
	
			rowData.add(commodityTypeInfo);

			if (table != null) {
				table.setVisible(false);
			}
			table = new JTable(rowData, columnNames) {
				/**
				 * 
				 */
				private static final long serialVersionUID = 1L;

				public boolean isCellEditable(int row, int column) {
					if (column == 0) {
						return false;
					} else {
						return true;
					}
				}
			};			
			table.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(new JComboBox<String>(getFatherableType())));
			table.setVisible(true);
			table.setBounds(64, 51, 511, 319);
			if (jsp != null) {
				jsp.setVisible(false);
			}
			jsp = new JScrollPane(table);
			jsp.setBounds(0, 51, 686, 319);
			jsp.setVisible(true);
			this.add(jsp);
		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(this, "无法连接到服务器\n" + e.getMessage());
			e.printStackTrace();
		}
	}
	
	private Vector<String> getFatherableType(){
		CommodityTypeBLService bl;
		Vector<String> result=new Vector<String>();
		try {
			bl = BLServiceManager.getCommodityTypeBLService(null);
			List<CommodityTypeVo> list=bl.getFatherAbleType();
			for(CommodityTypeVo vo:list){
				result.add(vo.get名称());
			}
		} catch (RemoteException | MalformedURLException | NotBoundException
				| SQLException e) {
			JOptionPane.showMessageDialog(this, "无法连接到服务器\n" + e.getMessage());
			e.printStackTrace();
		}
		return result;
	}
}
